﻿Friend Class Handler_Activities
    Private Shared mActivities As New Generic.Dictionary(Of Integer, MissingFeatureActivity)

    Public Shared Sub Start()
        CreateMissingFeatureActivities()
        SetupEvents(True)
    End Sub
    Public Shared Sub [Stop]()
        SetupEvents(False)
    End Sub

    Private Shared Sub SetupEvents(ByVal start As Boolean)
        If start Then
            AddHandler EntityHandlers.SharedItems.Updated, AddressOf EventHandler_SharedItemUpdated
            AddHandler EntityHandlers.SharedItems.Deleted, AddressOf EventHandler_SharedItemDeleted
        Else
            RemoveHandler EntityHandlers.SharedItems.Updated, AddressOf EventHandler_SharedItemUpdated
            RemoveHandler EntityHandlers.SharedItems.Deleted, AddressOf EventHandler_SharedItemDeleted
        End If
    End Sub

    Private Shared Sub EventHandler_SharedItemUpdated(ByVal item As Entities.SharedItem)
        Try
            If mActivities.ContainsKey(item.Id) Then
                'item already has an activity in the todo-list. Should we remove it??
                Dim clsActivity As MissingFeatureActivity = mActivities(item.Id)
                clsActivity.Item = item
                If item.HasMediaInfo Then
                    Dim clsMI As Entities.MediaInfo = EntityHandlers.MediaInfo.GetBySharedItemId(item.Id)
                    If clsMI IsNot Nothing Then
                        If clsMI.FeatureInfoStatus <> Entities.FeatureInfoStatus.MissingNeedsAction Then
                            RemoveActivity(clsActivity)
                            Return
                        End If
                    End If
                End If
                Framework.Activities.ToDoProvider.OnActivityUpdated(clsActivity)
            ElseIf Not item.HasMediaInfo AndAlso ShouldItemHaveMediaInfo(item) Then
                'item should have media info and its totally missing, add activity...
                Dim clsActivity As New MissingFeatureActivity(item)
                mActivities.Add(item.Id, clsActivity)
                Framework.Activities.ToDoProvider.Add(clsActivity)
            ElseIf ShouldItemHaveMediaInfo(item) Then
                'item should have media info - make sure it's complete...
                Dim clsMI As Entities.MediaInfo = EntityHandlers.MediaInfo.GetBySharedItemId(item.Id)
                If clsMI IsNot Nothing AndAlso clsMI.FeatureInfoStatus = Entities.FeatureInfoStatus.MissingNeedsAction Then
                    Dim clsActivity As New MissingFeatureActivity(item)
                    mActivities.Add(item.Id, clsActivity)
                    Framework.Activities.ToDoProvider.Add(clsActivity)
                ElseIf clsMI Is Nothing Then
                    Dim clsActivity As New MissingFeatureActivity(item)
                    mActivities.Add(item.Id, clsActivity)
                    Framework.Activities.ToDoProvider.Add(clsActivity)
                End If
            End If
        Catch ex As Exception
            Framework.Misc.HandleException(ex)
        End Try
    End Sub
    Private Shared Function ShouldItemHaveMediaInfo(ByVal item As Entities.SharedItem) As Boolean
        Select Case item.ItemType
            Case Entities.ItemType.Movies, Entities.ItemType.TVSeries, Entities.ItemType.MusicAlbums, Entities.ItemType.MusicFiles
                Return True
            Case Else
                Return False
        End Select
    End Function
    Private Shared Sub RemoveActivity(ByVal activity As MissingFeatureActivity)
        mActivities.Remove(activity.Item.Id)
        activity.Close()
    End Sub
    Private Shared Sub EventHandler_SharedItemDeleted(ByVal item As Entities.SharedItem)
        Try
            If mActivities.ContainsKey(item.Id) Then RemoveActivity(mActivities(item.Id))
        Catch ex As Exception
            Framework.Misc.HandleException(ex)
        End Try
    End Sub

    Private Shared Sub CreateMissingFeatureActivities()
        Dim aItems As Generic.List(Of Entities.SharedItem)
        aItems = EntityHandlers.SharedItems.GetAllByItemTypeAndMissingFeatureNeedingAction(Entities.ItemType.Movies)

        If aItems.Count > 0 Then
            Dim aActivities As New Generic.List(Of Framework.Activities.ActivityBase)
            For Each clsItem As Entities.SharedItem In aItems
                Dim clsActivity As New MissingFeatureActivity(clsItem)
                aActivities.Add(clsActivity)
                mActivities.Add(clsItem.Id, clsActivity)
            Next
            Framework.Activities.ToDoProvider.AddRange(aActivities)
        End If
    End Sub
End Class

Friend Class MissingFeatureActivity
    Inherits Framework.Activities.ActivityBase

    Private mItem As Entities.SharedItem
    Private mClosed As Boolean = False

    Friend Sub New(ByVal item As Entities.SharedItem)
        mItem = item
    End Sub


    Public Property Item() As Entities.SharedItem
        Get
            Return mItem
        End Get
        Friend Set(ByVal value As Entities.SharedItem)
            mItem = value
        End Set
    End Property

    Public Overrides ReadOnly Property Closed() As Boolean
        Get
            Return mClosed
        End Get
    End Property
    Public Sub Close()
        mClosed = True
        Framework.Activities.ActivityBase.OnActivityUpdated(Me)
    End Sub

    Public Overrides ReadOnly Property Id() As String
        Get
            Return mItem.Id.ToString
        End Get
    End Property

    Public Overrides ReadOnly Property Information() As String
        Get
            Return String.Format("Item ""{0}"" needs feature information ({1})!", mItem.Name, mItem.Comment)
        End Get
    End Property

    Public Overrides ReadOnly Property Timestamp() As Date
        Get
            Return mItem.Added
        End Get
    End Property

    Public Overrides ReadOnly Property Type() As String
        Get
            Return "Missing Feature Info"
        End Get
    End Property

    Public Overrides ReadOnly Property Image() As System.Drawing.Image
        Get
            Select Case mItem.ItemType
                Case Entities.ItemType.Movies
                    Return My.Resources.Movie.ToBitmap
                Case Else
                    Return Nothing
            End Select
        End Get
    End Property

    Public Overrides ReadOnly Property ImageKey() As String
        Get
            Select Case mItem.ItemType
                Case Entities.ItemType.Movies
                    Return "Movie"
                Case Else
                    Return Nothing
            End Select
        End Get
    End Property

    Public Overrides Sub ShowHandleUI(ByVal owner As System.Windows.Forms.Form)
        Dim clsView As New FHandleMissingFeatureActivity
        clsView.Show(Me)
    End Sub
End Class